package com.lixingye.meowstar.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lixingye.meowstar.common.AppResult;
import com.lixingye.meowstar.common.ResultCode;
import com.lixingye.meowstar.dao.BoardMapper;
import com.lixingye.meowstar.exception.ApplicationException;
import com.lixingye.meowstar.model.Board;
import com.lixingye.meowstar.service.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 猫大仙
 * @since 2025-01-14
 */
@Slf4j
@Service
public class BoardServiceImpl extends ServiceImpl<BoardMapper, Board> implements IBoardService {
    @Autowired
    private BoardMapper boardMapper;

    private List<Board> getByNum(@Param("num") Integer num){
        List<Board> boards = boardMapper.selectList(new LambdaQueryWrapper<Board>()
                .eq(Board::getState, 0)
                .eq(Board::getDeleteState, 0)
                .orderByAsc(Board::getSort)
                .le(Board::getSort, num)
        );
        return boards;
    }


    @Override
    public List<Board> selectByNum(Integer num) {
        if(num <= 0 || num>9){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return getByNum(num);
    }

    @Override
    public void addOneArticleCountById(Long id) {
        if(id == null || id<=0){
            log.error(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        Board board = boardMapper.selectById(id);
        if(board == null){
            log.error(ResultCode.ERROR_IS_NULL.toString() + ",board id = "+id);
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //更新帖子数量
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()+1);
        int i = boardMapper.updateById(updateBoard);
        if(i!=1){
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public void deleteOneArticleCountById(Long id) {
        if(id == null || id<=0){
            log.error(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        Board board = boardMapper.selectById(id);
        if(board == null){
            log.error(ResultCode.ERROR_IS_NULL.toString() + ",board id = "+id);
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //更新帖子数量
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()-1);
        if(updateBoard.getArticleCount()<0){
            updateBoard.setArticleCount(0);
        }
        int i = boardMapper.updateById(updateBoard);
        if(i!=1){
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public Board getById(Long id) {
        if(id == null || id<=0){
            log.error(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        Board board = boardMapper.selectById(id);
        return board;
    }
}
